<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	    		xmlns:ui="http://java.sun.com/jsf/facelets"
	  			xmlns:h="http://java.sun.com/jsf/html"
	  			xmlns:f="http://java.sun.com/jsf/core"
	  			xmlns:bf="http://www.bssmanager.com/jsf"           
                xmlns:cs="http://myfaces.apress.com/custom"
				template="/WEB-INF/layouts/standard.xhtml"
				xmlns:a4j="http://richfaces.org/a4j"
				xmlns:rich="http://richfaces.org/rich"
				xmlns:c="http://java.sun.com/jstl/core">
	
	<ui:define name="formheader">
		<h2>Room Allocation</h2>
	</ui:define>
	
	<ui:define name="formGlobal" >
		<bf:dateSelector controller="#{roomOccupancyController}" date="date" ajaxUpdate="false"/>
	</ui:define>
	
	<ui:define name="content">		
	
	<h:messages />
		<h:form id="roomOccForm" >
		
		<h:commandButton value="Continue Check-in" 
					action="#{roomOccupancyController.continueAdmission}" 
					/>	
		
		<h:panelGrid columns="2" rendered="#{roomOccupancyController.forCurrentAdmission}">
			<a4j:commandButton value="Continue Check-in" 
					action="#{roomOccupancyController.continueAdmission}" 
					/>		
			<a4j:commandLink value="Cancel Check-in"  oncomplete="document.location.href='roomOccupancy.jsf'" />
		</h:panelGrid>
		
		<c:set var="itemsPerRow" value="5"/>
		
		<h2>ROOMS</h2>
		<hr/>
		<h:inputHidden id="selectedPatientId" value="#{roomOccupancyController.selectedPatientId}"/>
		
		<h:inputHidden id="selectedRoomId" value="#{roomOccupancyController.roomBooking.room.id}"/>
		
		<h:inputHidden id="selectedBedId" value="#{roomOccupancyController.bedBooking.bed.id}"/>
		
		<ui:repeat var="building" value="#{roomOccupancyController.buildings}">
			<h3 style="text-decoration: underline;" align="center" >#{building.name}</h3>
			<ui:repeat var="floor" value="#{arrayCreator[building.floors]}" >
				<h4 style="text-decoration: underline;">Floor #{floor}</h4>
					<h:panelGroup>														
						<h:panelGrid columns="#{itemsPerRow}"  frame="box">
							<c:set var="found" value="0"/>
							<ui:repeat value="#{building.rooms}" var="room">
								<h:panelGroup layout="block" rendered="#{not room.dormitory and room.floor eq floor}">
								
									<h:panelGroup>
										<div class="itemCell" style="#{room.occupied ? 'background-color: gray;':''}">																									
												<span style=" #{room.occupied ? ' color: white;':''}">
													<c:choose>
														<c:when test="#{room.occupied}">
															<ui:repeat value="#{room.roomBookings}" var="roomBooking">
																<c:if test="#{roomBooking.room.id eq room.id}">
																	<table width="100%">
																		<tr>
																			<td valign="top">
																				<div>#{room.roomNumber}</div>
																				<a4j:commandLink reRender="selectedPatientPanel"
																						onclick="document.getElementById('roomOccForm:selectedPatientId').value='#{roomBooking.admission.patient.id}';"
																						oncomplete="Richfaces.showModalPanel('selectedPatientPanel');"
																						value="#{roomBooking.admission.patient.firstName} #{roomBooking.admission.patient.middleName} #{roomBooking.admission.patient.lastName}"																				
																						style="color: white;"
																						/>
																			</td>
																			<td align="right">
																				<h:graphicImage value="/bss/images/saved?id=#{roomBooking.admission.patient.id}" width="45" height="45"/>
																			</td>
																		</tr>
																	</table>
																</c:if>
															</ui:repeat>
														</c:when>
														<c:otherwise>
															<table width="100%">
																<tr>
																	<td valign="top">
																	<div>#{room.roomNumber} <h:selectBooleanCheckbox rendered="${roomOccupancyController.forCurrentAdmission}" value="${room.selected}" /> </div>
																	</td>
																	<td valign="top" align="right">
																		<a4j:commandButton rendered="#{not roomOccupancyController.forCurrentAdmission}"
																							reRender="selectedRoomPanel" image="/images/key-small.png"
																						onclick="document.getElementById('roomOccForm:selectedRoomId').value='#{room.id}';"
																						oncomplete="Richfaces.showModalPanel('selectedRoomPanel');"
																						action="#{roomOccupancyController.selectRoom}" />																		
																	</td>
																</tr>
															</table>
														</c:otherwise>
													</c:choose>																								
												</span>										
										</div>
									</h:panelGroup>
									<c:set var="found" value="1"/>
								</h:panelGroup>															
							</ui:repeat>
						</h:panelGrid>
						<c:if test="#{found eq 0}" >
							No Rooms found
						</c:if>
				</h:panelGroup>	
			</ui:repeat>
		</ui:repeat>
		
		<h:panelGroup id="roomPanel">
		
		
		
		<c:forEach items="#{roomOccupancyController.buildings}" var="building">
			<h3 style="text-decoration: underline;" align="center" >#{building.name}</h3>
			<c:forEach begin="1" end="#{building.floors}" var="floor">
				<h4 style="text-decoration: underline;">Floor #{floor}</h4>
				<h:panelGroup>														
						<h:panelGrid columns="#{itemsPerRow}"  frame="box">
							<c:set var="found" value="0"/>
							<c:forEach items="#{building.rooms}" var="room">
								<c:if test="#{not room.dormitory and room.floor eq floor}">
									<h:panelGroup>
										<div class="itemCell" style="#{room.occupied ? 'background-color: gray;':''}">																									
												<span style=" #{room.occupied ? ' color: white;':''}">
													<c:choose>
														<c:when test="#{room.occupied}">
															<c:forEach items="#{room.roomBookings}" var="roomBooking">
																<c:if test="#{roomBooking.room.id eq room.id}">
																	<table width="100%">
																		<tr>
																			<td valign="top">
																				<div>#{room.roomNumber}</div>
																				<a4j:commandLink reRender="selectedPatientPanel"
																						onclick="document.getElementById('roomOccForm:selectedPatientId').value='#{roomBooking.admission.patient.id}';"
																						oncomplete="Richfaces.showModalPanel('selectedPatientPanel');"
																						value="#{roomBooking.admission.patient.firstName} #{roomBooking.admission.patient.middleName} #{roomBooking.admission.patient.lastName}"																				
																						style="color: white;"
																						/>
																			</td>
																			<td align="right">
																				<h:graphicImage value="/bss/images/saved?id=#{roomBooking.admission.patient.id}" width="45" height="45"/>
																			</td>
																		</tr>
																	</table>
																</c:if>
															</c:forEach>
														</c:when>
														<c:otherwise>
															<table width="100%">
																<tr>
																	<td valign="top">
																	<div>#{room.roomNumber} <h:selectBooleanCheckbox rendered="${roomOccupancyController.forCurrentAdmission}" value="${room.selected}" /> </div>
																	</td>
																	<td valign="top" align="right">
																		<a4j:commandButton rendered="#{not roomOccupancyController.forCurrentAdmission}"
																							reRender="selectedRoomPanel" image="/images/key-small.png"
																						onclick="document.getElementById('roomOccForm:selectedRoomId').value='#{room.id}';"
																						oncomplete="Richfaces.showModalPanel('selectedRoomPanel');"
																						action="#{roomOccupancyController.selectRoom}" />																		
																	</td>
																</tr>
															</table>
														</c:otherwise>
													</c:choose>																								
												</span>										
										</div>
									</h:panelGroup>
									<c:set var="found" value="1"/>
								</c:if>															
							</c:forEach>
						</h:panelGrid>
						<c:if test="#{found eq 0}" >
							No Rooms found
						</c:if>
				</h:panelGroup>				
			</c:forEach>
		</c:forEach>
		
		</h:panelGroup>		
		<hr/>
	
		<br/>
		<br/>
		<h2>DORMITORIES</h2>
		<hr/>
		
		
		
		<h:panelGroup id="bedPanel">
			<c:forEach items="#{roomOccupancyController.buildings}" var="building">
				<h:panelGroup>											
					<c:forEach items="#{building.rooms}" var="room">
						<c:if test="#{room.dormitory}">
							<h:panelGroup>
								<h5 style="text-decoration: underline;">#{building.name} - Dormitory #{room.roomNumber}</h5>
								<h:panelGrid columns="#{itemsPerRow}" frame="box" >
									<c:forEach items="#{room.beds}" var="bed">
										<h:panelGroup >
											<div class="itemCell" style="#{bed.occupied ? 'background-color: gray;':''}">
												<span style=" #{bed.occupied ? ' color: white;':''}"> 
													<c:choose>
														<c:when test="#{bed.occupied}">
															<c:forEach items="#{bed.bedBookings}" var="bedBooking">
																<c:if test="#{bedBooking.bed.id eq bed.id}">
																	<table width="100%"> 
																		<tr>
																			<td valign="top">
																				<div>#{bed.bedNumber}</div>
																				<a4j:commandLink reRender="selectedPatientPanel"																						
																						onclick="document.getElementById('roomOccForm:selectedPatientId').value='#{bedBooking.admission.patient.id}';"
																						oncomplete="Richfaces.showModalPanel('selectedPatientPanel');"
																						value="#{bedBooking.admission.patient.firstName} #{bedBooking.admission.patient.middleName} #{bedBooking.admission.patient.lastName}"
																						style="color: white;"/>
																			</td>
																			<td align="right">
																				<h:graphicImage value="/bss/images/saved?id=#{bedBooking.admission.patient.id}" width="45" height="45"/>
																			</td>
																		</tr>																	
																	</table>																	
																</c:if>
															</c:forEach>
														</c:when>
														<c:otherwise>
															<table width="100%">
																<tr>
																	<td valign="top">
																	${bed.bedNumber} <h:selectBooleanCheckbox rendered="${roomOccupancyController.forCurrentAdmission}" value="${bed.selected}" />
																	</td>
																	<td valign="top" align="right">
																		<a4j:commandButton rendered="#{not roomOccupancyController.forCurrentAdmission}"
																		 reRender="selectedBedPanel" image="/images/key-small.png"
																						onclick="document.getElementById('roomOccForm:selectedBedId').value='#{bed.id}';"
																						oncomplete="Richfaces.showModalPanel('selectedBedPanel');"
																						action="#{roomOccupancyController.selectBed}" />																		
																	</td>
																</tr>
															</table>
														</c:otherwise>
													</c:choose>
												</span>
											</div>
										</h:panelGroup>
										<c:set var="found" value="1"/>
									</c:forEach>
								</h:panelGrid>
								<c:if test="#{empty room.beds}" >
									No Beds found
								</c:if>								
							</h:panelGroup>							
						</c:if>															
					</c:forEach>										
				</h:panelGroup>
				<hr/>
			</c:forEach>		
		</h:panelGroup>
		</h:form>
		
		<rich:modalPanel id="selectedPatientPanel" minHeight="100" minWidth="150"
			height="300" width="500" zindex="2000"  >
			<f:facet name="header">
				<h:outputText value="Patient"/>
			</f:facet>
							
				<div>#{roomOccupancyController.patient.firstName} #{roomOccupancyController.patient.middleName} #{roomOccupancyController.patient.lastName}</div>
					
				<br/>		
				<a href="#" onclick="Richfaces.hideModalPanel('selectedPatientPanel');">Hide</a>
			
		</rich:modalPanel>
		
		<rich:modalPanel id="selectedRoomPanel" minHeight="100" minWidth="150"
			height="300" width="500" >
			<f:facet name="header">
				<h:outputText value="Room Alocator"/>
			</f:facet>
			<div class="modalPanelInternal">				
				<h:form>				
					<table width="100%" cellpadding="0">
						<tr>
							<td colspan="2" style="background-color: #FFBF80; font-weight: bolder; font-size: 10pt;" align="left">
								Selected Room
							</td>
						</tr>
						<tr>
							<td align="left" colspan="2">
								<h:outputText value="#{roomOccupancyController.roomBooking.room.roomNumber} (Rate is Rs #{roomOccupancyController.roomBooking.room.rate})" style="font-weight: bolder;"/>
							</td>
						</tr>
						<tr>
							<td align="left" colspan="1">
								<h:outputLabel value="Proposed Rate: "/>
							</td>
							<td>
								<h:inputText value="#{roomOccupancyController.roomBooking.rate}" style="font-weight: bolder;"/>
							</td>
						</tr>
						<tr>
							<td colspan="2" style="background-color: #FFBF80; font-weight: bolder; font-size: 10pt;" align="left">
								Select Patient
							</td>
						</tr>	
						<tr>
							<td colspan="2" align="left" >
								<bf:admissionSelector idd="rpSelector" controller="#{roomOccupancyController}" selectMethod="select" reRender="selectedRoomOwner1,selectedRoomOwner2,btnAR"/>
							</td>
						</tr>
						<tr>
							<td colspan="2" style="background-color: #FFBF80; font-weight: bolder; font-size: 10pt;" align="left">
								Bookings
							</td>
						</tr>
						<tr>
							<td colspan="2" align="left">
								<h:panelGrid columns="2" id="selectedRoomOwner1">
									<h:outputLabel value="Name: "/>
									<h:outputText style="color:blue; font-weight:bold;" value="#{roomOccupancyController.roomBooking.admission.patient.name}"/>
								</h:panelGrid>
								<h:panelGroup id="selectedRoomOwner2">
									<h:outputLabel value="Previous Bookings:"/>
									<h:panelGrid columns="2" border="1">
										<h:panelGroup>
											<h:outputLabel value="Rooms:"/>
											<ul>
												<c:forEach items="#{roomOccupancyController.roomBooking.admission.roomBookings}" var="roomBooking">
													<li>
														#{roomBooking.room.roomNumber}
													</li>
												</c:forEach>
											</ul>										
										</h:panelGroup>
										<h:panelGroup >
											<h:outputLabel value="Bed:"/>
											<ul>
												<c:forEach items="#{roomOccupancyController.roomBooking.admission.bedBookings}" var="bedBooking">
													<li>
														#{bedBooking.bed.bedNumber}
													</li>
												</c:forEach>
											</ul>
										</h:panelGroup>
									</h:panelGrid>
								</h:panelGroup>
							</td>
						</tr>
						<tr>
							<td align="center" colspan="1">
								<a4j:commandButton id="btnAR" disabled="#{roomOccupancyController.roomBooking.admission.id le 0}"
								action="#{roomOccupancyController.allocateRoom}" 
								value="Allocate Selected Room" oncomplete="Richfaces.hideModalPanel('selectedRoomPanel');"
								reRender="roomPanel"/>
							</td>
							<td align="center" colspan="1"  >
								<a4j:commandButton value="Cancel"
								oncomplete="Richfaces.hideModalPanel('selectedRoomPanel'); "
								action="#{roomOccupancyController.cancelBooking}"
								reRender="selectedRoomPanel"/>
							</td>
						</tr>										
					</table>					
				</h:form>
			</div>
		</rich:modalPanel>
		
		
		<rich:modalPanel id="selectedBedPanel" minHeight="100" minWidth="150"
			height="300" width="500" >
			<f:facet name="header">
				<h:outputText value="Bed Alocator"/>
			</f:facet>
			<div class="modalPanelInternal">				
				<h:form>				
					<table width="100%" cellpadding="0">
						<tr>
							<td colspan="2" style="background-color: #FFBF80; font-weight: bolder; font-size: 10pt;" align="left">
								Selected Bed
							</td>
						</tr>
						<tr>
							<td align="left" colspan="2">
								<h:outputText value="#{roomOccupancyController.bedBooking.bed.bedNumber} (Rate is Rs #{roomOccupancyController.bedBooking.bed.room.rate})" style="font-weight: bolder;"/>
							</td>
						</tr>
						<tr>
							<td align="left" colspan="1">
								<h:outputLabel value="Proposed Rate: "/>
							</td>
							<td>
								<h:inputText value="#{roomOccupancyController.bedBooking.rate}" style="font-weight: bolder;"/>
							</td>
						</tr>
						<tr>
							<td colspan="2" style="background-color: #FFBF80; font-weight: bolder; font-size: 10pt;" align="left">
								Select Patient
							</td>
						</tr>	
						<tr>
							<td colspan="2" align="left" >
								<bf:admissionSelector idd="bpSelector" controller="#{roomOccupancyController}" selectMethod="select" reRender="selectedBedOwner1,selectedBedOwner2,btnAB"/>
							</td>
						</tr>
						<tr>
							<td colspan="2" style="background-color: #FFBF80; font-weight: bolder; font-size: 10pt;" align="left">
								Bookings
							</td>
						</tr>
						<tr>
							<td colspan="2" align="left">
								<h:panelGrid columns="2" id="selectedBedOwner1">
									<h:outputLabel value="Name: "/>
									<h:outputText style="color:blue; font-weight:bold;" value="#{roomOccupancyController.bedBooking.admission.patient.name}"/>
								</h:panelGrid>
								<h:panelGroup id="selectedBedOwner2">
									<h:outputLabel value="Previous Bookings:"/>
									<h:panelGrid columns="2" border="1">
										<h:panelGroup>
											<h:outputLabel value="Rooms:"/>
											<ul>
												<c:forEach items="#{roomOccupancyController.roomBooking.admission.roomBookings}" var="roomBooking">
													<li>
														#{roomBooking.room.roomNumber}
													</li>
												</c:forEach>
											</ul>										
										</h:panelGroup>
										<h:panelGroup >
											<h:outputLabel value="Bed:"/>
											<ul>
												<c:forEach items="#{roomOccupancyController.bedBooking.admission.bedBookings}" var="bedBooking">
													<li>
														#{bedBooking.bed.bedNumber}
													</li>
												</c:forEach>
											</ul>
										</h:panelGroup>
									</h:panelGrid>
								</h:panelGroup>
							</td>
						</tr>
						<tr>
							<td align="center" colspan="1">
								<a4j:commandButton id="btnAB" disabled="#{roomOccupancyController.bedBooking.admission.id le 0}"
									action="#{roomOccupancyController.allocateBed}" 
									value="Allocate Selected Bed"
									oncomplete="Richfaces.hideModalPanel('selectedBedPanel');"
									reRender="bedPanel"/>
							</td>
							<td align="center" colspan="1"  >
								<a4j:commandButton value="Cancel"
									oncomplete="Richfaces.hideModalPanel('selectedBedPanel'); "
									action="#{roomOccupancyController.cancelBooking}"
									reRender="selectedBedPanel"/>
							</td>
						</tr>										
					</table>					
				</h:form>
			</div>
		</rich:modalPanel>
	</ui:define>
</ui:composition>